package org.lex.string;

import java.io.PrintWriter;

public class ClassPrinter extends DefaultPrinter<Class<?>> {
	public static ClassPrinter getInstance() {
		return INSTANCE;
	}

	private static final ClassPrinter INSTANCE = new ClassPrinter();

	@Override
	public void format(Class<?> type, PrintWriter out) {
		if (type.isArray()) {
			out.print("array ");
			try {
				Class<?> cl = type;
				int dimensions = 0;
				while (cl.isArray()) {
					dimensions++;
					cl = cl.getComponentType();
				}
				out.print(cl.getName());
				for (int i = 0; i < dimensions; i++) {
					out.print("[]");
				}
			} catch (Throwable e) { /* FALLTHRU */
			}
		} else {
			this.printClass(type, out);
		}
		out.flush();
	}

	private void printClass(Class<?> clz, PrintWriter out) {
		if (clz.isAnnotation()) {
			out.print("annotation");
		}
		if (clz.isEnum()) {
			out.print("enum");
		}
		if (clz.isInterface()) {
			out.print("interface");
		}
		if (false == clz.isPrimitive()) {
			out.print("class");
		}
		if (clz.isSynthetic()) {
			out.print("synthetic");
		}
		out.print(' ');
		out.print(clz.getName());
		out.flush();
	}
}
